package com.vcms.controller.customer.states;

import com.vcms.controller.customer.TransactionController;
import com.vcms.controller.customer.CoinReceiver;
import com.vcms.exception.VMCSException;

public class FaultState extends TransactionState {

	public FaultState(TransactionController transactionController){
		setTransactionController(transactionController);
	}
	
	@Override
	public boolean handleDisplayCustomerPanel() {
		System.out.println("[FaultState] - Incorrect state to handle display customer panel!");
		
		return false;
	}
	
	@Override
	public boolean handleStartTransaction() {
		System.out.println("[FaultState] - Incorrect state to handle start transaction!");
		
		return false;
	}
	
	@Override
	public boolean handleProcessMoneyReceived() {
		System.out.println("[FaultState] - Incorrect state to handle process money received!");
		
		return false;
	}
	
	@Override
	public boolean handleCompleteTransaction() {
		System.out.println("[FaultState] - Incorrect state to complete transaction!");
		
		return false;
	}
	
	@Override
	public boolean handleTerminateTransaction() {
		System.out.println("[FaultState] - Incorrect state to terminate transaction!");
		
		return false;
	}
	
	@Override
	public boolean handleTerminateFault() {
		System.out.println("Inside Fault State Handle - Terminate Fault");
		TransactionController transactionController = this.getTransactionController();
		String sourceFault = transactionController.getFaultSource();
		CoinReceiver coinReceiver = transactionController.getCoinReceiver();
		
		if(sourceFault != null) {
			if(sourceFault.equalsIgnoreCase("DispenseController")) {
				coinReceiver.refundCash();
			}else if(sourceFault.equalsIgnoreCase("ChangeGiver")) {
				try {
					coinReceiver.storeCash();
				}catch(VMCSException e){
					System.out.println("Handled in Fault State - " + e.getMessage());
				}
			}else if(sourceFault.equalsIgnoreCase("CoinReceiver")) {
				
			}else {
				System.out.println("Invalid fault from source.");
			}
		}
		
		return true;
	}
	
	@Override
	public boolean handleRefreshCustomerPanel() {
		System.out.println("[FaultState] - Incorrect state to handle refresh customer panel!");
		
		return false;
	}

	public void printState(){
		System.out.println("- Fault State -");
	}
}
